package battleGame;

import java.util.Random;

public class Vector2 
{
	static Random rand = new Random();
	double x;
	double y;
	
	public Vector2()
	{
		x = 0;
		y = 0;
	}
	
	public Vector2( Vector2 otro )
	{
		x = otro.x;
		y = otro.y;
	}
	
	public Vector2( double otraX, double otraY )
	{
		x = otraX;
		y = otraY;
	}
	
	public Vector2 rotate(double angle)
	{
		double oldx = x;
		double oldy = y;
		double cos = Math.cos( angle );
		double sin = Math.sin( angle );
		x = oldx * cos - oldy * sin;
		y = oldx * sin + oldy * cos;
		
		normalize();
		return this;
	}
	
	public double magnitude()
	{
		return Math.sqrt( x * x + y * y );
	}
	
	public Vector2 normalize()
	{
		double mag = magnitude();
		if( mag > 0.01 )
		{
			x /= mag;
			y /= mag;
		}
		return this;
	}

	static Vector2 suma( Vector2 uno, Vector2 dos )
	{
		return new Vector2( uno.x + dos.x, uno.y + dos.y );
	}
	
	static Vector2 minus( Vector2 minuendo, Vector2 sustraendo )
	{
		return new Vector2( minuendo.x - sustraendo.x, minuendo.y - sustraendo.y );
	}

	static double dot( Vector2 uno, Vector2 dos )
	{
		return uno.x * dos.x + uno.y * dos.y;
	}

	static Vector2 mul( Vector2 uno, Vector2 dos )
	{
		return new Vector2( uno.x * dos.x, uno.y * dos.y );
	}
	
	static Vector2 mul( Vector2 uno, double numero )
	{
		return new Vector2( uno.x * numero, uno.y * numero );
	}
	
	public int getGridCol()
	{
		return ( (int)(x + Renderer.TILE_SIZE / 2) ) / (int)Renderer.TILE_SIZE;
	}
	
	public int getGridRow()
	{
		return ( (int)(y + Renderer.TILE_SIZE / 2 ) ) / (int)Renderer.TILE_SIZE;
	}
	
	public static Vector2 GetVector2FromColRow( int col, int row )
	{
		return new Vector2( (double) col * Renderer.TILE_SIZE, (double) row * Renderer.TILE_SIZE);
	}
	
	public static Vector2 GetRandomPoint()
	{
		return new Vector2( rand.nextDouble() * Renderer.WINDOW_SIZE_X, rand.nextDouble() * Renderer.WINDOW_SIZE_Y );
	}
	
	public static double GetAngleBetween( Vector2 uno, Vector2 dos )
	{
		uno.normalize();
		dos.normalize();
		return Math.acos( Vector2.dot( uno, dos ) );
	}
}
